package com.zph.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import com.angel.base.utils.AngelObject;
import com.angel.base.utils.Transformation;
import com.zph.dao.JSONUtil;
import com.zph.dao.ParseList;
import com.zph.dao.ResumeToTxt;
import com.zph.entity.CVObject;
import com.zph.entity.Constants;
//import com.zph.search.TalentSearch;

/**
 * 实时解析接口
 *
 */
@WebService(serviceName="ServiceStart")
public class ServiceStart {  
	private  String publishUrl = Constants.publishUrl;
	
	/**
	 * 具体解析简历文件
	 * 
	 * @param 文件服务器存放路径
	 * @return 解析结果JSON串
	 */
	public String getResumeInfo(String filePathname) {
		CVObject map = new CVObject();
		try {
			ResumeToTxt.parseResumeNow(map, filePathname);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONUtil.ObjectToJSON(map);
	}

	/**
	 * 对外发布的解析简历文件的接口
	 * 
	 * @return
	 */
	@WebMethod
	public String parseResume(String filePathname) {
 		return getResumeInfo(filePathname);
	}
	
	/**
	 * 对外发布的解析简历文件的接口 -- 格式转换
	 * 
	 * @return
	 */
	@WebMethod
	public String parseCv(String filePathname) {

		CVObject map = new CVObject();
		AngelObject cv = new AngelObject();
		try {
			//解析
			ResumeToTxt.parseResumeNow(map, filePathname);
			//格式转换
			  cv = Transformation.trans(map); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONUtil.ObjectToJSON(cv);  
	}
	
	@WebMethod
	public String UploadFile(DataHandler handler){
		 try {
			InputStream is = handler.getInputStream();   
			    OutputStream os = new FileOutputStream(new File("D:/111.txt"));   
			    byte[] b = new byte[100000];   
			    int bytesRead = 0;   
			    while ((bytesRead = is.read(b)) != -1) {   
			    os.write(b, 0, bytesRead);   
			    }   
			    System.out.println("3");  
			    os.flush();   
			    os.close();   
			    is.close(); 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return publishUrl;
	}
	
	/**
	 * 对外发布的解析寻访记录的接口
	 * 
	 * @return
	 */
	@WebMethod
	public String parseList(String filePathname) {
		return ParseList.parse(filePathname);
	}

	/**
	 * 对外发布的搜索人才的接口
	 * 
	 * @return
	 */
//	@WebMethod
//	public String searchTalent(String url, String type, int pageNum, int pageTotal, String sort) {
//		return TalentSearch.searchUser(url, type, pageNum, pageTotal, sort);
//	}

	/**
	 * 对外发布的导入联系人
	 * 
	 * @return
	 */
	@WebMethod
//	public String importList(int userId, int companyId, String filePath) {
//		return ImportList.importList(userId, companyId, filePath);
//	}

	public static void main(String[] args) {
		// 即时解析与后台脱机解析TYPE
		int type[] = { 1, 2, 3, 4 };
		ExecutorService m_threadPool; // 线程池
		m_threadPool = Executors.newFixedThreadPool(10);
		for (int i = 0, len = type.length; i < len; i++) {
			m_threadPool.execute(new ServiceStart().new MyThread(type[i]));
		}
	}

	/**
	 * 启动即时解析
	 */
	private void parseNow() {
		Endpoint.publish(publishUrl, new ServiceStart());
		// Endpoint.publish("http://localhost:9002/ParseServer/ServiceStart",
		// new ServiceStart());
		System.out.println("service success");
	}

	/**
	 * 启动脱机解析
	 */
	private void parseBehind() {
		ResumeToTxt.parseResumeBehind();
	}

	private class MyThread implements Runnable {
		private int type = 0;

		public MyThread(int type) {
			this.type = type;
		}
  
		public void run() {
			switch (this.type) {
			case 1:
				parseNow();
				break;
			case 2:
				parseBehind();
				break;
			case 3:
				ResumeToTxt.parseRecordBehind();
				break;
			case 4:
				try {
					ResumeToTxt.parseListBehind();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
		}
	}
	
	/**
	 *批量解析建立入口 
	 */
	
	public CVObject PLparseCv(String filePathname){

		CVObject map = new CVObject();
		try {
			ResumeToTxt.parseResumeNow(map, filePathname);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	
	}
	
	
	
}
